﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Bangle.Model;

namespace Bangle.BLL
{
    //Sys_User
    public partial class Sys_UserBLL
    {
        public UserSession UserLogin(string Account, string Password)
        {
            UserSession Session = new UserSession();
            Sys_User model = dal.UserLogin(Account, Password);

            if (model == null)
            {
                return null;
            }

            Session.LoginName = model.Account;
            //Session.PageSize = 30;
            Session.RoleId = model.RoleId;
            Session.UserID = model.UserId;
            Session.UserName = model.UserName;

            IDictionary<string, string> UserMenuOperation = new Dictionary<string, string>();

            UtilityBLL ubll = new UtilityBLL();
            int rcount = 0;
            DataTable dt = ubll.GetListByPage("View_Sys_OperationList", "*", "RoleId", "AllValid=1 and RoleId=" + model.RoleId, 100000, 1, 1, out rcount);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string ModuleCode = dt.Rows[i]["ModuleCode"].ToString();
                string OperationTypeCode = dt.Rows[i]["OperationTypeCode"].ToString();
                string sKey = ModuleCode + "#" + OperationTypeCode;
                if (!UserMenuOperation.ContainsKey(sKey))
                {
                    UserMenuOperation.Add(ModuleCode + "#" + OperationTypeCode, ModuleCode);
                }
            }
            Session.UserMenuOperation = UserMenuOperation;

            Sys_Role role_model = new Sys_Role();
            Sys_RoleBLL role_bll = new Sys_RoleBLL();
            role_model = role_bll.GetModel(model.RoleId);
            if (role_model != null)
            {
                Session.RoleName = role_model.RoleName;
                Session.RoleCode = role_model.RoleCode;
            }

            return Session;
        }
    }
}